<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/11
 * Time: 15:13
 */

namespace app\common\model;

use function Sodium\add;
use think\Db;
use think\Model;

class ShopCartGoodsModel extends Model
{
    //添加购物车
    public function addShopCart ($goods_id,$vip,$userid,$shop_id )
    {

        $map['shop_id'] = $shop_id;
        $map['status'] = 1;
        $map['goods_id']= $goods_id;
            //如果有状态为1 的 返回
        $batch_id = DB::table('ajw_slave_stock')
            ->where($map)
            ->value('batch_id');
        if (!$batch_id)
        {  //当不存在的时候返回空
            return false;
        }
        //查询出价格等
        $data = DB::table('ajw_main_stock')
            ->alias('a')
            ->join('ajw_goods h','a.goods_id = h.goods_id')
            ->where('a.batch_id',$batch_id)
            ->field(' a.goods_id  ,a.iprice ,a.iiprice,a.iiiprice,a.ivprive,h.name  ,h.imgurl,h.jianjie as content ,h.unit')
            ->select();

        $data =   $data->toArray();

        $data = $data[0];
        $data = $data +  Array('price' => $data['iprice']);

           if ($vip =="1")
           {
               $data = $data +  Array('price' => $data['iprice']);
           }
           elseif  ($vip == "2")
           {
               $data = $data +  Array('price' => $data['iiprice']);
           }
           elseif  ($vip == "3")
           {
               $data = $data +  Array('price' => $data['iiiprice']);
           }else
           {
               $data = $data +  Array('price' => $data['ivprice']);
           }
         unset($data["iprice"]);
         unset($data["iiprice"]);
         unset($data["iiiprice"]);
         unset($data["ivprice"]);


         //查询购物车中有没有相同的商品
        $map2['goods_id'] = $goods_id;
        $map2['batch_id'] = $batch_id;
        $goods = Db::table("ajw_shop_cart")
            ->where($map2)
            ->find();
         if ($goods)
         {
             //更新
             $res = Db::table("ajw_shop_cart")
                 ->where('cart_id',$goods["cart_id"])
                 ->setInc('count');

         }else
         {
             //添加
             $res = Db::table("ajw_shop_cart")
                 ->insert(["goods_id" => $data["goods_id"],
                     "batch_id"=>$batch_id,
                     "imgurl" => $data["imgurl"],
                     "name" => $data["name"],
                     "content" =>$data["content"],
                     "price" =>$data["price"],
                     "count" => 1 ,
                     "unit" => $data["unit"],
                     "userid" => $userid
                 ]);
         }


        return $res;
    }

    //购物车列表
    public function shopCartList ($user_id)
    {

        $data = Db::table("ajw_shop_cart")
            ->field('cart_id ,goods_id,imgurl,name,content,price,unit,count')
            ->where('userid',$user_id)
            ->select();

        return $data;


    }

    //增加数量
    public function jiaCount ($cart_id)
    {
        $res = Db::table("ajw_shop_cart")
            ->where('cart_id',$cart_id)
            ->setInc('count');

        if ($res==true)
        {
            return json(['code'=>1,'msg'=>'添加成功','data'=> ""]);
        }else
        {
            return json(['code'=>201,'msg'=>'添加失败','data'=> ""]);
        }
    }
    //减少数量
    public function jianCount ($cart_id)
    {
        $res = Db::table("ajw_shop_cart")
            ->where('cart_id',$cart_id)
            ->setDec('count');

        if ($res==true)
        {
            return json(['code'=>1,'msg'=>'减少成功','data'=> ""]);
        }else
        {
            return json(['code'=>201,'msg'=>'减少失败','data'=> ""]);
        }
    }


    public function cartDel($cart_id)
    {

            $cart_id = explode(',', $cart_id);
            $res = Db::table('ajw_shop_cart')
                ->delete($cart_id);
        if ($res==true)
        {
            return json(['code'=>1,'msg'=>'删除成功','data'=> ""]);
        }else
        {
            return json(['code'=>201,'msg'=>'删除失败','data'=> ""]);
        }
    }



    public function jieshuanCart ($cart_id)
    {

        $cart_id = explode(',', $cart_id);

        $data = Db::table("ajw_shop_cart")
            ->field('cart_id,goods_id,imgurl,name,content,price,unit,count')
            ->where('cart_id ','in',$cart_id)
            ->select();



        return $data ->toArray();;
//        $allprice = $allprice[0];
//        $data= $data->toArray();
//        $data = $data + $allprice;
        return $data;


    }

    public function getaddress ($user_id)
    {
        $map['user_id'] = $user_id;
        $map['status'] = 1;
        $addressData = Db::table("ajw_goods_accept_address")
            ->field('id as addressid ,mobile,name,address,status')
            ->where($map)
            ->select();
        $addressData = $addressData->toArray();



        $allprice = DB::table("ajw_shop_cart")
            ->field('sum(price * count) as tital')
            ->select();
        $allprice=$allprice->toArray();
        $allprice = $allprice[0];
        if ($addressData)
        {
            $address =   $addressData[0]  + $allprice;
            return $address;
        }else
        {
            $address =   $addressData  + $allprice;
            return $address;
        }



    }
}


